/*
 * URL writer
 */
$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});
/*
 * ----------------------------------------
 */
var startButton = new Button('startButton');
var endButton = new Button('endButton');
var pointButton = new Button('pointButton');
var acceptButton = new Button('acceptButton');

var flagInterval = 300;

//vertexes--------------
var start = "";
var oldStart = "";
var end = "";
var oldEnd = "";
var points = "";
var list = new List();
var oldList = new List();
//----------------------

$(document).ready(function () {
	
	//kursor na mapie
	$('div#main').css('cursor', 'crosshair');
	//przyciski
	
	// dodawanie przyciskow
	$('div#buttons').append("<div id='"+startButton.name+"'/>");
	$('div#buttons').append("<div id='"+endButton.name+"'/>");
	$('div#buttons').append("<div id='"+pointButton.name+"'/>");
	$('div#buttons').append("<div id='"+acceptButton.name+"'/>");
	$('div#acceptButton').hide();
	setFlag(startButton, true);
	
	//kklikanie
	buttonClick(startButton);
	buttonClick(endButton);
	buttonClick(pointButton);
	$('.start').hide();
	$('.end').hide();
	$('.point').hide();
	$('.describe').hide();
	
	/*
	 * ----------------------------------
	 * flag picking
	 * ----------------------------------
	 */
	if("search" == $.getUrlVar('do')) {
		//start
		var s = $.getUrlVar('s');
		pickStartFlag(s, false);
		//end
		var e = $.getUrlVar('e');
		pickEndFlag(e, false);
		//points
		var p = $.getUrlVar('p');
		all = p.split("b");
		for(var i = 0 ; i < all.length ; i++)
			if(all[i] != "")
				pickPointFlag(all[i], false);
		
		//numerowanie punktow
		numeratePoints(start, list.toString(), end);
		
		//przycisk akceptacji
		if(start!="" && end !="")
			$('div#acceptButton').show();
		else
			$('div#acceptButton').hide();
	}
	/*
	 * --------------------------------------------------
	 */
	
	//przekierowanie z zaznaczonymi wierzcholkami
	$('div#'+acceptButton.name).click(function() {
		
		var path = "map?do=search&s="+start+"&e="+end+"&p="+list.toString();
		window.location = path;
		
	});
	//klik na wynik => zaznaczenie szlakow na mapie
	$('.oneResult').click(function() {
		resultsDeClick();
		var id = $(this).attr("id");
		var str = $('div#' +id+  ' h1').text();
		all = str.split("p");
		for(var i = 0 ; i < all.length ; i++){
			if(all[i] != "") {
				var tmp = all[i];
				var w = $('div#p'+tmp).width();
				$('#p'+tmp).css("background-position", -w - 10);		
			}
		}
	
	});
	
	//describe

	$('div.vertex').mouseenter(function(e) {		
		var id = $(this).attr("id");
		$('div#'+id+ ' .describe ').show(500);
        
        $('div#'+id+ ' .describe ').mouseout(function() {
                  $('div#'+id+ ' .describe ').hide(500);
          });
	});
	
	$('div.vertex').click(function() {
		var id = $(this).attr("id");
		//start setting
		if(startButton.flag){
			pickStartFlag(id, true);
		}
		
		//end settings
		else if(endButton.flag){
			pickEndFlag(id, true);
		}
		//points settings
		else if(pointButton.flag){
			pickPointFlag(id, true);
		}
		//numerowanie punktow
		numeratePoints(start, list.toString(), end);
		
		//przycisk akceptacji
		if(start!="" && end !="")
			$('div#acceptButton').show(300);
		else
			$('div#acceptButton').hide(300);
				
	});
	
	
	
	
	
	
});

//klikanie
function buttonClick(button) {
	$('div#'+button.name).click(function() {
		
		if(button.flag)
			return;
		
		inactiveAll();
		button.flag = true;		
		$('div#'+button.name).addClass("active");
		
	});
}
function inactiveAll() {
	startButton.flag = false;
	$('div#'+startButton.name).removeClass("active");
	endButton.flag = false;
	$('div#'+endButton.name).removeClass("active");
	pointButton.flag = false;
	$('div#'+pointButton.name).removeClass("active");
}
function setFlag(button, flag) {
	button.flag = flag;
	if(button.flag)
		$('div#'+button.name).addClass("active");
	else
		$('div#'+button.name).removeClass("active");
}
function Button(id2) {
	this.name = id2;
	this.flag = false;
}
//moja lista
function List() {
	this.tab=new Array();
	this.i = 0;
	this.add = function(string){
		this.tab[this.i] = string;
		this.i++;
	};
	this.remove = function(k){
		this.tab[k] = "";
	};
	this.toString = function() {
		var all = "";
		for(var k = 0 ; k < this.i ; k++)
			if(this.tab[k] != "")
				all += this.tab[k] + "b";
		return all;
	};
}
function numeratePoints(start, points, end) {
	//start
	$('div#'+start+ ' .start').html('<p>'+0+'</p>');
	
	//point
	all = points.split("b");
	var nr = 0;
	for(var i = 0 ; i < all.length ; i++) {
		if(all[i] != ""){
			nr++;
			$('div#'+all[i]+ ' .point').html('<p>'+nr+'</p>');
		}
	}
	//end
	$('div#'+end+ ' .end').html('<p>'+ (++nr) +'</p>');
	
	$('.point p').hide();
	$('.start p').hide();
	$('.end p').hide();
	
	if(!(start == "" || end == "")) {
		$('.point p').show();
		$('.start p').show();
		$('.end p').show();
	}
}
function pickStartFlag(id, b) {
	oldStart = start;
	if(id == start)				
		start = "";
	else	
		start = id;
	//css
	if(b) {
		if(start != "")
			$('div#'+start+ ' .start').show(flagInterval);
		if(oldStart != "")
			$('div#'+oldStart+ ' .start').hide(flagInterval);
	}
	else {
		if(start != "")
			$('div#'+start+ ' .start').show();
		if(oldStart != "")
			$('div#'+oldStart+ ' .start').hide();
	}
}
function pickEndFlag(id, b) {
	oldEnd = end;
	if(id == end)
		end = "";
	else
		end = id;
	//css
	if(b) {
		if(end != "")
			$('div#'+end+ ' .end').show(flagInterval);
		if(oldEnd != "")
			$('div#'+oldEnd+ ' .end').hide(flagInterval);
	}
	else {
		if(end != "")
			$('div#'+end+ ' .end').show();
		if(oldEnd != "")
			$('div#'+oldEnd+ ' .end').hide();
	}
}

function pickPointFlag(id, b) {
	var r = true;
	for(var i = 0 ; i < list.i ; i++)
		if(id == list.tab[i]){
			list.remove(i);
			oldList.add(id);
			r = false;
			break;
		}
	if(r) {
		list.add(id);
		for(var i = 0 ; i < oldList.i ; i++)
			if(id == oldList.tab[i]){
				oldList.remove(i);
				break;
			}
	}
	for(var i = 0 ; i < list.i ; i++) {
		if(b) {
			if(list.tab[i] != "")
				$('div#'+list.tab[i]+ ' .point').show(flagInterval);
			if(oldList.tab[i] != "")
				$('div#'+oldList.tab[i]+ ' .point').hide(flagInterval);	
		}
		else {
			if(list.tab[i] != "")
				$('div#'+list.tab[i]+ ' .point').show();
			if(oldList.tab[i] != "")
				$('div#'+oldList.tab[i]+ ' .point').hide();			
		}
	}
	
}
function resultsDeClick() {
	$('.path').css("background-position", 0);	
}